﻿using SpyApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using PagedList;


namespace SpyApp.Controllers
{
    public class Test1Controller : Controller
    {
        
        public UserRepository userRepository;
        List<Message> listMessage = new List<Message>();

        public Test1Controller()
        {
            this.userRepository = new UserRepository();
            for (int i = 0; i < 35; i++)
            {
                listMessage.Add(FakeMake.FakeMessage());
            }
        }

        [AllowAnonymous]
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            IEnumerable<Message> query = listMessage;
            
            // page
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            // search string
            if (!string.IsNullOrEmpty(searchString))
            {
                query = listMessage.Where(i =>
                    i.UserMessage.Contains(searchString)
                    || i.NumberSendReceive.Contains(searchString));
            }

            // chuyển asc, desc
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NumberSortParm = String.IsNullOrEmpty(sortOrder) ? "number_desc" : "number";
            ViewBag.ContextParm = sortOrder == "context" ? "context_desc" : "context";
            switch (sortOrder)
            {
                case "number":
                    query = listMessage.OrderBy(i => i.NumberSendReceive);
                    break;
                case "number_desc" :
                    query = listMessage.OrderByDescending(i => i.NumberSendReceive);
                    break;
                case "context":
                    query = listMessage.OrderBy(i => i.UserMessage);
                    break;
                case "context_desc":
                    query = listMessage.OrderByDescending(i => i.UserMessage);
                    break;
            }
            // page
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(query.ToPagedList(pageNumber, pageSize));
        }

        [AllowAnonymous]
        public ActionResult nghia()
        {
            return View();
        }
    }
}
